package net.dopan.pigframe.user.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.dopan.pigframe.core.base.controller.BaseController;
import net.dopan.pigframe.core.base.controller.response.ResponseBean;
import net.dopan.pigframe.core.model.SysZuulRoute;
import net.dopan.pigframe.core.utils.CommonConstant;
import net.dopan.pigframe.core.utils.Query;
import net.dopan.pigframe.user.service.SysZuulRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @Package: net.dopan.pigframe.user.controller
 * @Title: ZuulRouteController
 * @Description: 动态路由配置表
 * @author: 刘宽
 * @date: 2018/9/2 21:21
 */
@RestController
@RequestMapping("/route")
@Api(tags = "动态路由接口(API)", description = "动态路由相关操作")
public class ZuulRouteController extends BaseController {
    @Autowired
    private SysZuulRouteService sysZuulRouteService;
    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysZuulRoute
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询id对应的zuul", notes = "显示一条zuul")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id编号", required = true, dataType = "Long")
    })
    public SysZuulRoute get(@PathVariable Integer id) {
        return sysZuulRouteService.selectById(id);
    }

    /**
     * 分页查询信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/page")
    @ApiOperation(value = "列表", notes = "展示zuul列表")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return sysZuulRouteService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 添加
     *
     * @param sysZuulRoute 实体
     * @return success/false
     */
    @PostMapping
    @ApiOperation(value = "添加一条zuul", notes = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysZuulRoute", value = "sysZuulRoute对象实体", required = true, dataType = "SysZuulRoute")
    })
    public ResponseBean<Boolean> add(@RequestBody SysZuulRoute sysZuulRoute) {
        return new ResponseBean<>(sysZuulRouteService.insert(sysZuulRoute));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一条zuul", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id编号", required = true, dataType = "Long")
    })
    public ResponseBean<Boolean> delete(@PathVariable Integer id) {
        SysZuulRoute sysZuulRoute = new SysZuulRoute();
        sysZuulRoute.setId(id);
        sysZuulRoute.setUpdateTime(new Date());
        sysZuulRoute.setDelFlag(CommonConstant.STATUS_DEL);
        return new ResponseBean<>(sysZuulRouteService.updateById(sysZuulRoute));
    }

    /**
     * 编辑
     *
     * @param sysZuulRoute 实体
     * @return success/false
     */
    @PutMapping
    @ApiOperation(value = "编辑一条zuul", notes = "编辑")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysZuulRoute", value = "sysZuulRoute对象实体", required = true, dataType = "SysZuulRoute")
    })
    public ResponseBean<Boolean> edit(@RequestBody SysZuulRoute sysZuulRoute) {
        sysZuulRoute.setUpdateTime(new Date());
        return new ResponseBean<>(sysZuulRouteService.updateById(sysZuulRoute));
    }

    /**
     * 刷新配置
     *
     * @return success/fasle
     */
    @GetMapping("/apply")
    @ApiOperation(value = "刷新zuul配置", notes = "刷新zuul配置")
    public ResponseBean<Boolean> apply() {
        return new ResponseBean<>(sysZuulRouteService.applyZuulRoute());
    }
}
